﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using DocumentManagement.Models;
using DocumentManagement.Views;
using DocumentManagement.Views.YGD;
using System.Globalization;

namespace DocumentManagement.Controllers
{
    public class YuGuDanController : BaseController
    {
        [Authentication(PermissionLevel.YuGuDanOperationList)]
        public ActionResult Index()
        {
            return View();
        }


        [Authentication(PermissionLevel.YuGuDanEdit)]
        public ActionResult ShowYuGuDanEdit(Guid id)
        {
            var ygd = this.Model.GetYuGuDan(id);

            return View("EditYuGuDan", new EditYuGuDanViewData { YGD = ygd });
        }

        [Authentication(PermissionLevel.YuGuDanEdit)]
        public ActionResult EditYuGuDan(YuGuDan ygd)
        {
            this.Model.UpdateYuGuDan(ygd);

            return this.RedirectToAction("ShowYuGuDanDetail", new { id = ygd.id });
        }
        
        [Authentication(PermissionLevel.YuGuDanCreate)]
        public ActionResult ShowCreateYuGuDan()
        {
            return View("CreateYuGuDan");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [Authentication(PermissionLevel.YuGuDanCreate)]
        public ActionResult CreateYuGuDan(YuGuDan ygd, bool? zjgd)
        {
            bool needToBeDocumented = zjgd != null;

            bool isTemp = string.IsNullOrEmpty(ygd.ygbh);

            var newYGD = this.Model.CreateYuGuDan(ygd, this.AuthenticatedUser.id);

            if (isTemp || needToBeDocumented)
            {
                this.Model.DocumentYuGuDanDirectly(newYGD.id);
            }


            return RedirectToAction("ShowCreateYuGuDan");
        }

        [Authentication(PermissionLevel.YuGuDanAssgin)]
        public ActionResult ShowUnprocessedYuGuDan()
        {
            var ygds = this.Model.GetUnprocessedYuGuDan();
            var employees = this.Model.GetAllUsers();
            return View("UnprocessedYuGuDan", 
                new UnprocessedYuGuDanViewData() { YuGuDans = ygds, Employees = employees });
        }

        [Authentication(PermissionLevel.YuGuDanAssgin)]
        public ActionResult ProcessYuGuDan(Guid id, int state, int assigneeId)
        {
            var s = (YuGuDanState)state;

            switch (s)
            {
                case YuGuDanState.WeiChuLi:
                    break;
                case YuGuDanState.AssignedToEmployeeToCharge:
                    this.Model.AssignYuGuDanToEmployeeToChargeMoney(id, assigneeId);
                    break;
                case YuGuDanState.AssignedToEmployeeToGetReport:
                    this.Model.AssignYuGuDanToEmployeeToGetReport(id, assigneeId);
                    break;
                case YuGuDanState.DocumentedDirectly:
                    this.Model.DocumentYuGuDanDirectly(id);
                    break;
                default:
                    throw new InvalidOperationException("不支持该操作");
            }

            return RedirectToAction("ShowUnprocessedYuGuDan");
        }

        [Authentication(PermissionLevel.YuGuDanToReportProcess)]
        public ActionResult ShowCurrentUserAssignedYuGuDan()
        {
            var ygds = this.Model.GetAssignedYuGuDanByUserId(this.AuthenticatedUser.id);

            return View(
                "CurrentUserAssignedYuGuDan", 
                new CurrentUserAssignedYuGuDanViewData() { YuGuDans = ygds });
        }

        [Authentication(PermissionLevel.YuGuDanViewDetail)]
        public ActionResult ShowYuGuDanDetail(Guid id)
        {
            var ygd = this.Model.GetYuGuDan(id);

            return View("YuGuDanDetail", new YuGuDanDetailViewData() { YGD = ygd });
        }

        [Authentication(PermissionLevel.YuGuDanSearch)]
        public ActionResult ShowSearch()
        {
            return View("Search");
        }

        [Authentication(PermissionLevel.YuGuDanSearch)]
        public ActionResult Search(string ygbh, string wtly, string wtf, string xmzl, DateTime? gjshd, string wtr)
        {
            var ygds = this.Model.SearchYuGuDan(ygbh, wtly, wtf, xmzl, gjshd, wtr);
            return View("SearchResult", new SearchResultViewData() { YGDs = ygds });
        }

        [Authentication(PermissionLevel.YuGuDanViewDetail)]
        public ActionResult DownloadYuGuDanDocForZhongGuoBank(Guid id)
        {
            string file = this.Model.GenerateYuGuDanDocForZhongGuoBank(id);
            
            return this.File(file, "application/msword", "ZhongGuoBank.doc");
        }

        [Authentication(PermissionLevel.YuGuDanViewDetail)]
        public ActionResult DownloadYuGuDanDocForJianSheBank(Guid id)
        {
            string file = this.Model.GenerateYuGuDanDocForJianSheBank(id);

            return this.File(file, "application/msword", "JianSheBank.doc");
        }

        [Authentication(PermissionLevel.YuGuDanViewDetail)]
        public ActionResult DownloadYuGuDanDocForShenZhenBank(Guid id)
        {
            string file = this.Model.GenerateYuGuDanDocForShenZhenBank(id);

            return this.File(file, "application/msword", "ShenZhenBank.doc");
        }
    }
}
